<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once("Core.php");
class CI_Task extends CI_Core{
	function __construct(){
		parent::__construct();
		$this->database('default');
		$this->ci->load->library('Huashang/order');
	}
	//取消订单
	public function cancelOrder(){
		$rs=$this->get_all('order_sn',array('order_status'=>1,'pay_ment !='=>4,'add_time <='=>strtotime('-10 minutes')),'order',array('orderby'=>array('add_time'=>'desc'),'limit'=>10));
		foreach($rs as $v){
			$a=$this->ci->order->cancelOrder($v['order_sn']);
		}
	}
	//跟踪快递
	public function ship($id){
		if($id){//订单ID存在
			$rs = $this->crud->get_one('id,express_ment,express_number',array('id'=>$id,'ship_status'=>1),'order');
			$this->_express($rs);
		}else{
			$rs=$this->get_all('id,express_ment,express_number',array('ship_status'=>1,'add_time <='=>strtotime('-10 minutes')),'order',array('orderby'=>array('add_time'=>'desc'),'limit'=>50));
			foreach($rs as $v) $this->_express($v);
		}
	}
	private function _express($rs){
		if($rs==array()) return;
		$arr = $this->express($rs['express_ment'], $rs['express_number']);
		if(!$arr) return;
		if($arr['status']=='已签收'){//将订单更改为已收货状态
			$this->db->update('order', array('ship_status'=>3,'express_log'=>json_encode($arr)), array('id'=>$rs['id']));
		}else{
			$this->db->update('order', array('express_log'=>json_encode($arr)), array('id'=>$rs['id']));
		}
	}
}